const auth = require("../auth/auth");
const mzxxkDir = "/home/server/m-zxxk2.0/web";
const { exec, spawn } = require("child_process");

function deploySpawn() {
  let sp = spawn("yarn", ["run", "deploy"], {
    cwd: mzxxkDir,
  });
  sp.stdout.on("data", (data) => {
    console.log(data.toString("utf-8"));
  });

  sp.stderr.on("data", (data) => {
    console.log("nuxt打包报错:", data.toString("utf-8"));
  });

  sp.on("exit", (data) => {
    console.log("nuxt打包完毕，状态码:", data);
    sp = null;
  });
}

module.exports = function (req, res) {
  console.log("开始发布m-zxxk，请稍后...");
  if (auth.checkIp(req)) {
    //发布前端nuxt部分
    deploySpawn();
  }
  let result = {
    msg: "finish",
  };
  res.end(JSON.stringify(result), "utf-8");
};
